package com.ilop.sthome.mvp.present.scene;

import android.content.Context;
import android.content.Intent;

import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.common.command.SendAutoCommand;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.mvp.contract.scene.SceneDetailContract;
import com.ilop.sthome.ui.activity.scene.AutomationDetailActivity;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.AutomationDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.SceneDaoUtil;
import com.ilop.sthome.utils.tools.ByteUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

/**
 * @author skygge
 * @date 2020-01-19.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class SceneDetailPresenter extends BasePresenterImpl<SceneDetailContract.IView> implements SceneDetailContract.IPresent {

    private Context mContext;
    private SendAutoCommand mSendAuto;

    private int mSceneId;
    private int mDeleteAutomationId;
    private String mDeviceName;
    private String mSceneCode;
    private String mAutoCode;
    private AutomationBean mAutomation;

    public SceneDetailPresenter(Context mContext, String deviceName, int sceneId) {
        this.mContext = mContext;
        this.mDeviceName = deviceName;
        this.mSceneId = sceneId;
        DeviceInfoBean mDeviceInfoBean = DeviceDaoUtil.getInstance().findGatewayByDeviceName(deviceName);
        mSendAuto = new SendAutoCommand(mContext, mDeviceInfoBean);
    }

    @Override
    public void getSceneName() {
        SceneBean mScene = SceneDaoUtil.getInstance().findSceneBySid(mSceneId, mDeviceName);
        if (mScene == null){
            mView.finishActivity();
            return;
        }
        String mSceneName;
        switch (mSceneId){
            case 0:
                mSceneName = mContext.getString(R.string.home_mode);
                break;
            case 1:
                mSceneName = mContext.getString(R.string.out_mode);
                break;
            case 2:
                mSceneName = mContext.getString(R.string.sleep_mode);
                break;
            default:
                if (mScene.getModleName() == null){
                    mSceneName = "";
                }else {
                    mSceneName = mScene.getModleName();
                }
                break;
        }
        mView.showSceneName(mSceneName);
    }

    @Override
    public void getAutoList() {
        List<AutomationBean> mSceneList = AutomationDaoUtil.getInstance().findAllAutoWithDefault(mDeviceName);
        if (mSceneList.size()>0){
            mView.showAutoList(mSceneList);
        }else {
            mView.withoutAuto();
        }
    }

    @Override
    public void onUpdateAuto(AutomationBean automation) {
        mAutomation = automation;
        mSceneCode = CoderUtils.getNewSceneId(mSceneId, automation.getSceneId());
        String autoId = CoderUtils.setCodeSupplement2Byte(Integer.toHexString(automation.getMid()));
        String finalCode = CoderUtils.getNewCode(mSceneCode, automation.getInputList(), automation.getOutputList());
        String finalCRC = ByteUtil.stringToCRC(finalCode);
        mAutoCode = autoId + finalCode + finalCRC;
        mSendAuto.increaseAutomation(mAutoCode.toUpperCase());
        mAutomation.setCrcCord(finalCRC);
    }

    @Override
    public void onUpdateSuccess() {
        if (mAutomation!=null && mSceneCode!=null) {
            mAutomation.setCode(mAutoCode);
            mAutomation.setSceneId(mSceneCode);
            AutomationDaoUtil.getInstance().getAutomationDao().update(mAutomation);
        }
        getAutoList();
    }

    @Override
    public void onClickExecution(int mid) {
        mSendAuto.autoClick(mid);
    }

    @Override
    public void skipActivityToUpdate(AutomationBean automationBean) {
        Intent intent = new Intent();
        intent.setClass(mContext, AutomationDetailActivity.class);
        intent.putExtra(CommonId.KEY_DEVICE_NAME, automationBean.getDeviceName());
        intent.putExtra(CommonId.KEY_MODIFY, true);
        intent.putExtra(CommonId.KEY_SCENE_DETAIL, automationBean);
        mView.skipActivity(intent);
    }

    @Override
    public void onDeleteAutomation(int automationId){
        this.mDeleteAutomationId = automationId;
        mSendAuto.deleteAutomation(automationId);
    }

    @Override
    public void deleteSuccess() {
        if(mDeleteAutomationId>-1){
            AutomationDaoUtil.getInstance().deleteByMid(mDeleteAutomationId, mDeviceName);
            mDeleteAutomationId = -1;
            getAutoList();
        }
    }
}
